import threading
import time


class MyThread(threading.Thread):
    def __init__(self, threadID, name, counter):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter

    def run(self):
        print("开启线程： {}".format(self.name))
        threadLock.acquire()
        print_time(self.name, self.counter, 3)
        threadLock.release()


def print_time(threadName, delay, counter):
    while counter:
        time.sleep(delay)
        print("{}, {}".format(threadName, time.ctime(time.time())))
        counter -= 1


def runThread():
    threads = []
    thread1 = MyThread(1, "Thread-1", 1)
    thread2 = MyThread(2, 'Thread-2', 2)
    thread1.start()
    thread2.start()
    threads.append(thread1)
    threads.append(thread2)

    # 等待子线程完成后结束
    for t in threads:
        t.join()
    print("退出主线程")


threadLock = threading.Lock()
if __name__ == '__main__':
    runThread()
